Skip to content

Switch to proxy URL to handle API requests to demo, #PG-5029#876

Merged
AltamashShaikh merged 4 commits into
livefrom
PG-5029-fix-cors
Apr 10, 2026
Merged

Switch to proxy URL to handle API requests to demo, #PG-5029#876
AltamashShaikh merged 4 commits into
livefrom
PG-5029-fix-cors

Conversation

@lachiebol
Copy link
Copy Markdown
Contributor

Description

Please include a description of this change and which issue it fixes. If no issue exists yet please include context and what problem it solves.

Checklist

  • [✔] I have understood, reviewed, and tested all AI outputs before use
  • [✔] All AI instructions respect security, IP, and privacy rules

Review

Comment thread app/templates/api-swagger.twig Outdated
window.onload = function () {
let pluginSpec = {{ pluginSpecJson|raw }};
let pluginSpecJson = {{ pluginSpecJson|raw }};
let proxyBaseUrl = window.location.origin + '/demo-proxy';
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let proxyBaseUrl = window.location.origin + '/demo-proxy';
let proxyBaseUrl = '/demo-proxy';

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made that change, it also collapses the server field down to this
image

I changed it to 'demo' rather than 'demo-proxy', proxy felt too technical for a user facing page.

Comment thread app/routes/page.php

$proxiedResponse = DemoProxy::get($targetUrl, $request->getHeaderLine('Authorization'));

$response->getBody()->write($proxiedResponse['body']);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should add a try/catch here

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, I also realised we return errors with a 200 OK? At the moment it doesn't break anything in Swagger, technically we could catch in the proxy and throw something else

Comment thread app/routes/page.php Outdated
});

$app->get('/demo-proxy/{path:.*}', function (Request $request, Response $response, $args) {
$path = ltrim($args['path'] ?? '', '/');
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

main concern is that it now becomes a proxy to query demo.matomo.cloud without any CORS error

We can add a check that requestURL path should be index.php and Module should be API along with CSRF token.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have added some URL validation so only the API can be queried.

Demo is a public instance, are we exposing anything that isn't already available with this proxy? For the CSRF token, we don't have a session on developer docs, so not sure where we can store that

Copy link
Copy Markdown
Contributor

@AltamashShaikh AltamashShaikh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this correct ?

Image

Copy link
Copy Markdown
Contributor

@AltamashShaikh AltamashShaikh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

left few comments

@lachiebol
Copy link
Copy Markdown
Contributor Author

Is this correct ?

Image

@AltamashShaikh That's the new proxy endpoint that swagger will make requests against, do you think it should still show demo.matomo.cloud? I might check if I can change the text but not the URL

Copy link
Copy Markdown
Contributor

@AltamashShaikh AltamashShaikh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lachiebol Good to add some tests with the help of AI

@lachiebol
Copy link
Copy Markdown
Contributor Author

@AltamashShaikh Sorry, just got around to this, but we don't have PHPUnit setup at all, and no testing workflow for this. Do we want to introduce testing pipelines for developer docs?

@AltamashShaikh
Copy link
Copy Markdown
Contributor

@AltamashShaikh Sorry, just got around to this, but we don't have PHPUnit setup at all, and no testing workflow for this. Do we want to introduce testing pipelines for developer docs?

@lachiebol Create a follow-up ticket, we should add it at the end, nothing is needed now.

@AltamashShaikh AltamashShaikh merged commit 4846351 into live Apr 10, 2026
1 check passed
@AltamashShaikh AltamashShaikh deleted the PG-5029-fix-cors branch April 10, 2026 02:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

2 participants